{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:27.380643Z",
     "start_time": "2019-05-15T16:12:25.699672Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thu May 16 00:12:26 2019       \n",
      "+-----------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 390.48                 Driver Version: 390.48                    |\n",
      "|-------------------------------+----------------------+----------------------+\n",
      "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
      "|===============================+======================+======================|\n",
      "|   0  TITAN X (Pascal)    Off  | 00000000:02:00.0 Off |                  N/A |\n",
      "| 46%   75C    P2    87W / 250W |  10995MiB / 12196MiB |      0%      Default |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   1  TITAN X (Pascal)    Off  | 00000000:04:00.0 Off |                  N/A |\n",
      "| 54%   83C    P2    93W / 250W |  11671MiB / 12196MiB |     64%      Default |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   2  TITAN X (Pascal)    Off  | 00000000:83:00.0 Off |                  N/A |\n",
      "| 62%   83C    P2   193W / 250W |  12096MiB / 12196MiB |     92%      Default |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "|   3  TITAN X (Pascal)    Off  | 00000000:84:00.0 Off |                  N/A |\n",
      "| 51%   82C    P2   166W / 250W |   8144MiB / 12196MiB |     58%      Default |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "                                                                               \n",
      "+-----------------------------------------------------------------------------+\n",
      "| Processes:                                                       GPU Memory |\n",
      "|  GPU       PID   Type   Process name                             Usage      |\n",
      "|=============================================================================|\n",
      "|    0     44683      C   python                                      3289MiB |\n",
      "|    0    155760      C   python                                      4345MiB |\n",
      "|    0    158310      C   python                                      2297MiB |\n",
      "|    0    172338      C   /home/yzs/anaconda3/bin/python              1031MiB |\n",
      "|    1    139985      C   python                                     11653MiB |\n",
      "|    2     38630      C   python                                      5547MiB |\n",
      "|    2     43127      C   python                                      5791MiB |\n",
      "|    2    156710      C   python3                                      725MiB |\n",
      "|    3     14444      C   python3                                     1891MiB |\n",
      "|    3     43407      C   python                                      5841MiB |\n",
      "|    3     88478      C   /home/tangss/.conda/envs/py36/bin/python     379MiB |\n",
      "+-----------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:29.958567Z",
     "start_time": "2019-05-15T16:12:27.383299Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:47.137875Z",
     "start_time": "2019-05-15T16:12:29.962468Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Linear(in_features=10, out_features=1, bias=True)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = torch.nn.Linear(10, 1).cuda()\n",
    "net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:47.143709Z",
     "start_time": "2019-05-15T16:12:47.139895Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DataParallel(\n",
       "  (module): Linear(in_features=10, out_features=1, bias=True)\n",
       ")"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = torch.nn.DataParallel(net)\n",
    "net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:47.206714Z",
     "start_time": "2019-05-15T16:12:47.145069Z"
    }
   },
   "outputs": [],
   "source": [
    "torch.save(net.state_dict(), \"./8.4_model.pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:47.260076Z",
     "start_time": "2019-05-15T16:12:47.208314Z"
    }
   },
   "outputs": [],
   "source": [
    "new_net = torch.nn.Linear(10, 1)\n",
    "# new_net.load_state_dict(torch.load(\"./8.4_model.pt\")) # 加载失败"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:47.317397Z",
     "start_time": "2019-05-15T16:12:47.262131Z"
    }
   },
   "outputs": [],
   "source": [
    "torch.save(net.module.state_dict(), \"./8.4_model.pt\")\n",
    "new_net.load_state_dict(torch.load(\"./8.4_model.pt\")) # 加载成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T16:12:47.370299Z",
     "start_time": "2019-05-15T16:12:47.319323Z"
    }
   },
   "outputs": [],
   "source": [
    "torch.save(net.state_dict(), \"./8.4_model.pt\")\n",
    "new_net = torch.nn.Linear(10, 1)\n",
    "new_net = torch.nn.DataParallel(new_net)\n",
    "new_net.load_state_dict(torch.load(\"./8.4_model.pt\")) # 加载成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
